#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <windows.h>
#include <QMessageBox>
#include <QTimer>
void stopHook();
HHOOK keyHook=nullptr;
bool flag=false,flag1=false;
static long X,Y;
QTimer *timer;

extern "C"
{
    int WINAPI MessageBoxTimeoutA(IN HWND hWnd, IN LPCSTR lpText, IN LPCSTR lpCaption, IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds);
    int WINAPI MessageBoxTimeoutW(IN HWND hWnd, IN LPCWSTR lpText, IN LPCWSTR lpCaption, IN UINT uType, IN WORD wLanguageId, IN DWORD dwMilliseconds);
};
#ifdef UNICODE
#define MessageBoxTimeout MessageBoxTimeoutW
#else
#define MessageBoxTimeout MessageBoxTimeoutA
#endif

//键盘钩子过程
LRESULT CALLBACK keyProc(int nCode,WPARAM wParam,LPARAM lParam )
{
    //在WH_KEYBOARD_LL模式下lParam 是指向KBDLLHOOKSTRUCT类型地址
    KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam;
    //如果nCode等于HC_ACTION则处理该消息，如果小于0，则钩子子程就必须将该消息传递给 CallNextHookEx
    if(nCode == HC_ACTION){
        if(pkbhs->vkCode == 82 && GetAsyncKeyState(VK_CONTROL)& 0x8000 && GetAsyncKeyState(VK_SHIFT)&0x8000 &&flag){
            SetWindowPos(GetForegroundWindow(),HWND_NOTOPMOST,0,0,0,0,3);
            POINT lp;
            GetCursorPos(&lp);
            X=lp.x;
            Y=lp.y;
            QString str="Recorded!Point at("+QString::number(lp.x)+","+QString::number(lp.y)+")";
            MessageBoxA(GetForegroundWindow(),str.toStdString().c_str(),"Info",0);
            flag=false;
        }
        else if(pkbhs->vkCode == 67 && GetAsyncKeyState(VK_CONTROL)& 0x8000 && GetAsyncKeyState(VK_SHIFT)&0x8000 &&flag1){
            timer->stop();
            MessageBoxA(GetForegroundWindow(),"AutoClick is stopped","Info",0);
            flag1=false;
        }
        return CallNextHookEx(keyHook, nCode, wParam, lParam);
    }
    return CallNextHookEx(keyHook, nCode, wParam, lParam);
}
void stopHook()//用来销毁定义的所有钩子
{

    if(keyHook!=nullptr)
    {
        UnhookWindowsHookEx(keyHook);//键盘钩子句不为空时销毁掉
        keyHook=nullptr;
    }
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    auto Boardhinstance=GetModuleHandleA(NULL);
    keyHook =SetWindowsHookEx( WH_KEYBOARD_LL,keyProc,Boardhinstance,0);
}

MainWindow::~MainWindow()
{
    delete ui;
    stopHook();
}


void MainWindow::on_pushButton_2_clicked()
{
    if(timer!=nullptr) timer->stop();
    HWND hwnd=GetForegroundWindow();
    SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,3);
    flag=true;
    flag1=false;
}

void MainWindow::on_pushButton_clicked()
{
    if(time.isNull()||time.isEmpty()) return;
    ShowWindow(GetForegroundWindow(),SW_MINIMIZE);
    flag=false;
    flag1=true;
    if(timer!=nullptr){
        delete timer;
        timer=nullptr;
    }
    timer=new QTimer();
    timer->setInterval(time.toInt()*1000);
    connect(timer,SIGNAL(timeout()),this,SLOT(timeoutProc()));
    timer->start();
    MessageBoxTimeoutA(GetForegroundWindow(),"AutoClick is running","info",0,0,3*1000);
}

void MainWindow::on_lineEdit_textChanged(const QString &arg1)
{
    this->time=arg1;
}

void MainWindow::timeoutProc()
{
    SetCursorPos(X,Y);
    mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
    Sleep(100);
    mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
}
